package P3;

import jmetal.base.Problem;
import jmetal.base.Solution;
import jmetal.util.JMException;
import TeamPursuit.MensTeamPursuit;
import TeamPursuit.SimulationResult;
import TeamPursuit.WomensTeamPursuit;


public class Cycling extends Problem{

    private double bestTime;
	public Cycling(String solutionType){
		numberOfVariables_   = 1;
		numberOfObjectives_  = 2;//----------------
		numberOfConstraints_ = 0;
	    problemName_         = "Cycling";
            bestTime = Integer.MAX_VALUE;

	    if(solutionType.compareTo("MenCycleSolutionType") == 0){
	    	solutionType_ = new MenCycleSolutionType(this);
	    }
	    else if(solutionType.compareTo("WomenCycleSolutionType") == 0){
	    	solutionType_ = new WomenCycleSolutionType(this);
	    }
	    else{
	    	System.out.println("Error: solution type " + solutionType + " invalid") ;
	    	System.exit(-1) ;
	    }
	}
	@Override
	public void evaluate(Solution solution) throws JMException {
		// TODO Auto-generated method stub
		SimulationResult result = null;
                double time = 0;
		Strategy st = (Strategy)solution.getDecisionVariables()[0];
		try {
			if(this.getSolutionType().typename.equalsIgnoreCase("men")){
				MensTeamPursuit teamPursuit = new MensTeamPursuit();
				result = teamPursuit.simulate(st.getTransition(), st.getPacing());
                                time = result.getFinishTime();

			}
			else if(this.getSolutionType().typename.equalsIgnoreCase("women")){
				WomensTeamPursuit teamPursuit = new WomensTeamPursuit();
				result = teamPursuit.simulate(st.getTransition(), st.getPacing());
                                time = result.getFinishTime();
			}
			else{
		    	System.out.println("Error") ;
		    	System.exit(-1) ;
			}
                        if(time<bestTime)
                        {
                            bestTime = time;
                        }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		solution.setObjective(0, result.getFinishTime());
		double totalenergy=0.0;
		if(this.getSolutionType().typename.equalsIgnoreCase("men")){
			double[] energy = result.getEnergyRemaining();
			totalenergy = energy[0]+energy[1]+energy[2]+energy[3];
		}
		else if(this.getSolutionType().typename.equalsIgnoreCase("women")){
			double[] energy = result.getEnergyRemaining();
			totalenergy = energy[0]+energy[1]+energy[2];
		}
		solution.setObjective(1, - totalenergy);
                System.out.println("bestTime: " + bestTime);
	}
        public void evaluateConstraints(Solution solution) throws JMException {
            double time = solution.getObjective(0);
            int number = 0;
            if(bestTime - time < -1.5) {
                number = 1;
            }
            solution.setNumberOfViolatedConstraint(number);
            solution.setOverallConstraintViolation(bestTime - time);
        }

        
}
